package com.pic.mgt.syushin.executer;

import java.sql.Timestamp;
import java.util.Map;

import org.springframework.dao.DataAccessException;

import com.pic.mgt.dao.ITargetMessageDao;
import com.pic.mgt.dao.syushin.SyushinBusinessTargetActionDao;
import com.pic.mgt.executer.IGXTypeExecutionException;
import com.pic.mgt.executer.MessageTypeExecutionException;
import com.pic.mgt.executer.MessageUnitExecuter;
import com.pic.mgt.syushin.unit.SyushinMessage;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.operation.UnitOperationException;

public abstract class SyushinMessageUnitExecuter extends MessageUnitExecuter {
	
	protected ITargetMessageDao targetMessageDao;
	
	@Override
	protected void executeMessageType() throws MessageTypeExecutionException {
		SyushinMessage syushinMessage = (SyushinMessage)super.unit;
		
		// 更新資料庫
		try {
			// 新增 target message 資料
			insertTargetMessage(syushinMessage);
			// 更新 business target action 的最後交易時間
			// (送多方修改)
			updateBusinessTargetActionTxTime(syushinMessage);
		} catch (DataAccessException e) {
			throw new MessageTypeExecutionException(
					"Updating syushin target transaction '" + syushinMessage.getTransactionFilename() +
					"' record to database faild." ,e);
		}
		
		/*
		 *  移除 Message File 
		 *  (包含 Confirmation File,
		 *  	 Confirmation File For Transaction File,
		 *       Transaction File
		 *  三個檔)
		 */
		try {
			// throws UnitOperationException
			super.simpleMessageUnitOperation.removeUnit(syushinMessage);
		} catch (UnitOperationException e) {
			throw new MessageTypeExecutionException(
					"Syushin message '" + syushinMessage.getUnitName() +
					"' execution failed in removing message file process" ,e);
		}
		
		
		// 預留
		try {
			executeIGXType();
		} catch (IGXTypeExecutionException e) {
			throw new MessageTypeExecutionException("", e);
		}
	}

	protected void insertTargetMessage (SyushinMessage syushinMessage)
	throws DataAccessException {
		// 新增資料 (table: *_target_message)
		Map<String, Object> inserValuesMap = getInsertTargetMessageBasicValuesMap(syushinMessage);
		// throws DataAccessException
		this.targetMessageDao.insertBasic(inserValuesMap);
	}
	
	// (送多方新增)
	/**
	 * 更新 *_business_target_action table 內的最後交易時間。
	 * @param message
	 */
	private void updateBusinessTargetActionTxTime(Message message) {
		// (送多方修改)
		// 更新資料 (table: *_business_target_action)
		((SyushinBusinessTargetActionDao)this.businessTargetActionDao)
										.updateLastTxTimeByFilenameAndApName(
												new Timestamp(message.getTransactionTime().getTime()),
												message.getTransactionFilename(),
												((SyushinMessage)message).getApName());
	}
	
	protected abstract void executeIGXType() throws IGXTypeExecutionException;
	
	protected abstract Map<String, Object>
			getInsertTargetMessageBasicValuesMap(SyushinMessage syushinMessage);
	
	// --- getter and setter ---
	
	public void setTargetMessageDao(ITargetMessageDao targetMessageDao) {
		this.targetMessageDao = targetMessageDao;
	}
	
}
